﻿using AutoMapper;
using MediatR;
using Reporting.API.Application.CommandHandler.InvoiceManagements;
using Reporting.API.Application.Commands.Ticketmanagements;
using Reporting.Domain;
using Reporting.Errorcode;
using Reporting.Errorcode.ErrorCode;
using Reporting.Infrastructure.Interfaces.InvoiceManagements;
using Reporting.Infrastructure.Interfaces.Ticketmanagements;

namespace Reporting.API.Application.CommandHandler.Ticketmanagements
{
    public class DeleteTicketmanagementCommandHandler : IRequestHandler<DeleteTicketmanagementCommand, ApiResult>
    {
        private readonly ITicketmanagementRepository _ticketmanagementRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<DeleteTicketmanagementCommandHandler> _logger;

        public DeleteTicketmanagementCommandHandler(ILogger<DeleteTicketmanagementCommandHandler> logger, IMapper mapper, ITicketmanagementRepository ticketmanagementRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _ticketmanagementRepository = ticketmanagementRepository;
        }
        public async Task<ApiResult> Handle(DeleteTicketmanagementCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // id查询生产工单
                var Ticketmanagements = await _ticketmanagementRepository.GetAsync(x => x.Ticketmanagementid == request.Ticketmanagementid);
                if (Ticketmanagements == null)
                {
                    return ApiResult.Fail("删除失败，当前数据不存在", StatusCodeEnum.BadRequest);
                }
                // 删除生产工单
                var result = await _ticketmanagementRepository.DeleteAsync(Ticketmanagements);
                return ApiResult.Success(StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "到票删除失败");
                throw;
            }
        }
    }
}
